package util

import (
	"crypto/md5"
	"encoding/hex"
	"encoding/json"
)

// EncodeMD5 对字符串进行MD5加密
func EncodeMD5(value string) string {
	m := md5.New()
	m.Write([]byte(value))

	return hex.EncodeToString(m.Sum(nil))
}

func EncodeToString(value string) string {
	src := []byte(value)
	return hex.EncodeToString(src)
}

func DecodeString(value string) (string, error) {
	src, err := hex.DecodeString(value)
	if err != nil {
		return "", err
	}
	return string(src[:]), nil
}

// StrutToString 结构体转字符串
func StrutToString(value interface{}) (string, error) {
	r, err := json.Marshal(value)
	if err != nil {
		return "", err
	}
	return string(r), nil
}
